package com.gzdh.device.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.device.entity.DeviceType;
import com.gzdh.device.device.entity.MachineDevice;
import com.gzdh.device.device.service.DeviceTypeService;
import com.gzdh.device.device.service.MachineDeviceService;
import com.gzdh.device.shop.dto.ShopDeviceDTO;
import com.gzdh.device.shop.entity.ShopInfo;
import com.gzdh.device.shop.service.ShopInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/shop/device/v1")
public class ShopDeviceController {


    @Autowired
    MachineDeviceService machineDeviceService;

    @Autowired
    ShopInfoService shopInfoService;

    @Autowired
    DeviceTypeService deviceTypeService;

    @GetMapping("getDevice")
    public Result<Object> getDevice(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(name = "deviceSn", required = false) String deviceSn,
                                    @RequestParam(name = "shopId", required = false) String shopId) {
        Result<Object> result = new Result<>();
        IPage<MachineDevice> machineDeviceList = machineDeviceService.page(new Page<>(pageNo, pageSize),
                new QueryWrapper<MachineDevice>().lambda()
                        .like(StringUtils.isNotEmpty(deviceSn), MachineDevice::getDeviceSn, deviceSn)
                        .eq(StringUtils.isNotBlank(shopId), MachineDevice::getShopId, shopId)
                        .orderByDesc(MachineDevice::getCreateTime));
        List<ShopInfo> shopInfoList = shopInfoService.list();
        List<DeviceType> deviceTypeList = deviceTypeService.list();
        IPage<ShopDeviceDTO> shopDeviceDTOList = new Page<>();
        BeanUtils.copyProperties(machineDeviceList, shopDeviceDTOList);
        shopDeviceDTOList.setRecords(new ArrayList<>());
        for (MachineDevice goo : machineDeviceList.getRecords()) {
            ShopDeviceDTO shopDeviceDTO = new ShopDeviceDTO();
            shopDeviceDTO.setId(goo.getId());
            shopDeviceDTO.setShopId(goo.getShopId());
            shopDeviceDTO.setDeviceSn(goo.getDeviceSn());
            shopDeviceDTO.setCreateTime(goo.getCreateTime());
            shopDeviceDTO.setImei(goo.getImei());
            if (StringUtils.isEmpty(goo.getShopId())) {
                shopDeviceDTO.setShopName("未分配");
            } else {
                ShopInfo shopInfo = shopInfoList.stream().filter(shopInfo1 -> shopInfo1.getId().equals(goo.getShopId())).findFirst().orElse(null);
                if (shopInfo == null) {
                    shopDeviceDTO.setShopName("商户不存在");
                } else {
                    shopDeviceDTO.setShopName(shopInfo.getName());
                }
            }
            DeviceType deviceType = deviceTypeList.stream().filter(deviceType1 -> deviceType1.getDeviceTypeId().equals(goo.getDeviceType()))
                    .findFirst().orElse(null);
            if (deviceType != null) {
                shopDeviceDTO.setDeviceTypeName(deviceType.getDeviceName());
            } else {
                shopDeviceDTO.setDeviceTypeName("未知");
            }
            shopDeviceDTOList.getRecords().add(shopDeviceDTO);
        }
        result.setResult(shopDeviceDTOList);
        return result;
    }

    @PostMapping("updateShop")
    public Result<Object> updateShop(@RequestBody JSONObject jsonObject) {
        Result<Object> result = new Result<>();
        String shopId = jsonObject.getString("shopId");
        String deviceId = jsonObject.getString("id");
        machineDeviceService.lambdaUpdate().set(MachineDevice::getShopId, shopId).eq(MachineDevice::getId, deviceId)
                .update();
        return result;
    }
}
